package cn.itcast.shop.user.service;

import org.springframework.transaction.annotation.Transactional;

import cn.itcast.shop.user.dao.UserDao;
import cn.itcast.shop.user.vo.User;
import cn.itcast.shop.utils.MailUtils;
import cn.itcast.shop.utils.UUIDUtils;

@Transactional
public class UserService {
	private UserDao userDao;
	
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	/**
	 * 按用户名查询用户
	 * @param username 用户名
	 * @return
	 */
	public User findByUsername(String username){
		return userDao.findByUsername(username);
	}
	
	/**
	 * 添加用户
	 * @param user
	 */
	public void save(User user){
		//将数据存入到数据库
		user.setState(0);  // 0: 代表用户未激活.  1：代表用户已经激活
		String code =UUIDUtils.getUUID()+UUIDUtils.getUUID();
		user.setCode(code);
		userDao.save(user);
		//发送激活邮件！
		MailUtils.sendMail(user.getEmail(), code);
		
	}

	public User findByCode(String code) {
		User existUser= userDao.findByCode(code);
		return existUser;
	}

	public void update(User existUser) {
		userDao.update(existUser);		
	}

	public User login(User user) {
		User existUser = userDao.login(user);
		return existUser;
	}
	
	
}













